package com.hpjpw.idoapp.service;

import com.hpjpw.ido.domain.AnonymousComment;
import com.hpjpw.ido.domain.AnonymousMoment;
import com.hpjpw.ido.domain.AnonymousThumb;
import com.hpjpw.ido.service.IAnonymousCommentService;
import com.hpjpw.ido.service.IAnonymousMomentService;
import com.hpjpw.ido.service.IAnonymousThumbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IDoAnonymousService {
    @Autowired
    IAnonymousMomentService anonymousMomentService;
    @Autowired
    IAnonymousCommentService anonymousCommentService;
    @Autowired
    IAnonymousThumbService anonymousThumbService;

    /* 针对前端的撕逼墙动态请求服务，一次性返回相关数据 */
    public List<Map<String,Object>> momentListReSponse(){
        /* 查询消息列表，只需发送get请求以及是否分页即可 */
        List<AnonymousMoment> list = anonymousMomentService.selectAnonymousMomentList(new AnonymousMoment());
        /* 所有应该返回的结果 */
        List<Map<String, Object>> listReturn = new ArrayList<>();
        /* 撕逼墙只给出id content picture */
        for (AnonymousMoment item:list) {
            /* status=1说明审核通过 */
            if (item.getStatus().equals("1")) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", item.getId().toString());
                map.put("content", item.getContent());
                map.put("picture1", item.getPicture1());
                map.put("picture2", item.getPicture2());
                map.put("picture3", item.getPicture3());
                map.put("time", item.getTime().toString());
                AnonymousThumb anonymousThumb = new AnonymousThumb();
                anonymousThumb.setMomentId(item.getId());
                List<AnonymousThumb> anonymousThumbList = anonymousThumbService.selectAnonymousThumbList(anonymousThumb);
                /* 返回所有点赞者id */
                List<Map<String,String>> mapList = new ArrayList<>();
                for (AnonymousThumb thumb :anonymousThumbList) {
                    Map<String, String> stringMap = new HashMap<>();
                    stringMap.put("thumbedId",thumb.getThumbedId().toString());
                    mapList.add(stringMap);
                }
                map.put("thumbIds",mapList);
                /* 点赞者数量 */
                Integer thumbNum = anonymousThumbList.size();
                map.put("thumbNum", thumbNum.toString());
                /* 携带评论数据，初始返回三个评论数据 */
                /* 修改>>返回所有评论数据 */
                List<Map<String,String>> commentList = new ArrayList<>();
                AnonymousComment anonymousComment = new AnonymousComment();
                anonymousComment.setMomentId(item.getId());
                List<AnonymousComment> anonymousCommentList = anonymousCommentService.selectAnonymousCommentList(anonymousComment);
                /* 默认返回最多三条评论，修改>>返回所有评论 */
                int defaultComment = anonymousCommentList.size();
                for (int i = 0; i < defaultComment; i++) {
                    /* 如果当前评论为空则直接退出 */
                    /*if (i>=anonymousCommentList.size()){
                        break;
                    }*/
                    AnonymousComment comment = anonymousCommentList.get(i);
                    Map<String, String> mapOfComment = new HashMap<>();
                    mapOfComment.put("content",comment.getContent());
                    commentList.add(mapOfComment);
                }
                map.put("comments",commentList);
                /* 总评论数 */
                Integer commentNum = anonymousCommentList.size();
                map.put("commentNum", commentNum.toString());
                listReturn.add(map);
            }
        }
        /* 对结果做一次反转，即按照最近的时间排序，并将此结果返回 */
        /*Collections.reverse(listReturn);*/
        return listReturn;
    }

    /* 针对前端的撕逼墙具体动态的评论请求服务 */
    public List<Map<String,String>> commentListResponse(Long momentId){
        AnonymousComment anonymousComment = new AnonymousComment();
        anonymousComment.setMomentId(momentId);
        List<AnonymousComment> anonymousCommentList = anonymousCommentService.selectAnonymousCommentList(anonymousComment);
        List<Map<String,String>> listReturn = new ArrayList<>();
        /* 返回每一条评论的内容和动态id和评论id */
        for (AnonymousComment item:anonymousCommentList) {
            Map<String,String> map = new HashMap<>();
            map.put("momentId",item.getMomentId().toString());
            map.put("commentId",item.getId().toString());
            map.put("content", item.getContent());
            listReturn.add(map);
        }
        return listReturn;
    }

    /* 针对某一动态id(此处确定存在此动态，controller中添加安全校验)，返回该动态所有内容 */
    public Map<String,Object> momentResponse(Long momentId){
        Map<String, Object> map = new HashMap<>();
        AnonymousMoment item = anonymousMomentService.selectAnonymousMomentById(momentId);
        /* status=1说明审核通过 */
        if (item.getStatus().equals("1")) {
            map.put("id", item.getId().toString());
            map.put("content", item.getContent());
            map.put("picture1", item.getPicture1());
            map.put("picture2", item.getPicture2());
            map.put("picture3", item.getPicture3());
            map.put("time", item.getTime().toString());
            AnonymousThumb anonymousThumb = new AnonymousThumb();
            anonymousThumb.setMomentId(item.getId());
            List<AnonymousThumb> anonymousThumbList = anonymousThumbService.selectAnonymousThumbList(anonymousThumb);
            /* 返回所有点赞者id */
            List<Map<String,String>> list = new ArrayList<>();
            for (AnonymousThumb thumb :anonymousThumbList) {
                Map<String, String> stringMap = new HashMap<>();
                stringMap.put("thumbedId",thumb.getThumbedId().toString());
                list.add(stringMap);
            }
            map.put("thumbIds",list);
            /* 点赞者数量 */
            Integer thumbNum = anonymousThumbList.size();
            map.put("thumbNum", thumbNum.toString());
            /* 携带评论数据，初始返回三个评论数据 */
            List<Map<String,String>> commentList = new ArrayList<>();
            AnonymousComment anonymousComment = new AnonymousComment();
            anonymousComment.setMomentId(item.getId());
            List<AnonymousComment> anonymousCommentList = anonymousCommentService.selectAnonymousCommentList(anonymousComment);
            /* 返回所有评论 */
            for (int i = 0; i < anonymousCommentList.size(); i++) {
                AnonymousComment comment = anonymousCommentList.get(i);
                Map<String, String> mapOfComment = new HashMap<>();
                mapOfComment.put("content",comment.getContent());
                commentList.add(mapOfComment);
            }
            map.put("comments",commentList);
            /* 总评论数 */
            Integer commentNum = anonymousCommentList.size();
            map.put("commentNum", commentNum.toString());
        }
        return map;
    }
}
